package apoc.core;

import apoc.core.command.CommandApoc;
import apoc.core.core.creativetab.ApocTab;
import apoc.core.core.handler.GuiHandler;
import apoc.core.core.handler.PlayerHandler;
import apoc.core.core.handler.TickHandler;
import apoc.core.core.proxy.CommonProxy;
import apoc.core.lib.References;
import apoc.core.network.PacketPipeline;
import apoc.core.network.packet.SyncPlayerPacket;
import apoc.core.plugin.ApocPlugin;
import apoc.core.plugin.PluginManager;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraftforge.common.MinecraftForge;

@Mod(
        modid = References.MOD_ID,
        name = References.MOD_NAME)
public class ApocCore
{
    @Instance(References.MOD_ID)
    public static ApocCore instance;

    /*
    The Apoc Tab where Blocks and Items are displayed upon.
     */
    public static CreativeTabs apocTab = new ApocTab(CreativeTabs.getNextID(), References.MOD_ID);

    /*
    Pipeline for packet handling
     */
    public static final PacketPipeline packetPipeline = new PacketPipeline();

    @SidedProxy(
            clientSide = References.CLIENT_PROXY_LOCATION,
            serverSide = References.COMMON_PROXY_LOCATION)
    public static CommonProxy proxy;

    public static TickHandler tickHandler;

    @EventHandler
    public void serverStart(FMLServerStartingEvent event)
    {
        /* Registering ApocCommandHandler */
        event.registerServerCommand(new CommandApoc());
    }

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        /* Registering Tick Handler*/
        tickHandler = new TickHandler();
        FMLCommonHandler.instance().bus().register(tickHandler);

        /* Registering Player Handler*/
        MinecraftForge.EVENT_BUS.register(new PlayerHandler());

        /* Add preInit for all Plugins */
        for (ApocPlugin plugin : PluginManager.plugins())
            plugin.preInit(tickHandler, event);

        /* Load Proxy.
        * If you want a change in this file, make a pull request.
        */
        proxy.load();
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {

        packetPipeline.initalise();
        packetPipeline.registerPacket(SyncPlayerPacket.class);

        /* Add init for all Plugins */
        for (ApocPlugin plugin : PluginManager.plugins())
            plugin.init(packetPipeline, event);

        /*
        RegisterGUIHandler
         */
        NetworkRegistry.INSTANCE.registerGuiHandler(ApocCore.instance, new GuiHandler());

        proxy.registerRendering();
    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
        packetPipeline.postInitialise();

        /* Add postInit for all Plugins */
        for (ApocPlugin plugin : PluginManager.plugins())
            plugin.postInit(event);
    }
}